package com.bgs.eat.shop.service.impl;

import com.alipay.api.domain.Shop;
import com.bgs.eat.config.CommonResult;
import com.bgs.eat.fileManager.service.FileService;
import com.bgs.eat.menuBack.entity.MenuBackEntity;
import com.bgs.eat.page.MyPageHelper;
import com.bgs.eat.page.PageRequest;
import com.bgs.eat.page.PageResult;
import com.bgs.eat.rider.entity.UserProtal;
import com.bgs.eat.shop.entity.ShopEntity;
import com.bgs.eat.shop.mapper.ShopMapper;
import com.bgs.eat.shop.service.ShopService;
import com.bgs.eat.userBack.entity.UserBackEntity;
import com.bgs.eat.userProtal.entity.UserProtalEntity;
import com.bgs.eat.userProtal.mapper.UserProtalMapper;
import com.bgs.eat.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName ShopServiceImpl
 * @Description TODO
 * @Author lzy
 * @Date 2022/10/9 14:54
 **/
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    FileService fileService;

    @Autowired
    UserProtalMapper userProtalMapper;

    /**
    * @Description 申请店铺 上传相关图片
    * @Author lzy
    * @Date 2022/10/13 19:15
    * @Return com.bgs.eat.config.CommonResult
    * @param file
    * @param name
    */
    @Override
    public CommonResult applyUploadImage(MultipartFile file, String name) {
        Integer imageId = fileService.uniUploadFile(file);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("imageId", imageId);
        return CommonResult.ok(map);
    }

    /**
    * @Description 新增店铺（门户展示 店家申请个开通店铺）
    * @Author lzy
    * @Date 2022/10/16 20:38
    * @Return com.bgs.eat.config.CommonResult
    * @param shopEntity
    */
    @Override
    public CommonResult saveShop(ShopEntity shopEntity) {
        UserProtalEntity userProtalEntity = new UserProtalEntity();
        userProtalEntity.setId(shopEntity.getUserProtalId());
        UserProtalEntity userProtalEntity1 = userProtalMapper.selectOne(userProtalEntity);
        if(userProtalEntity1.getAuditId() != 5) {
            userProtalEntity.setAuditId(2);
            userProtalMapper.updateByPrimaryKeySelective(userProtalEntity);
        }
        shopEntity.setAmount(0);
        shopEntity.setScore("5");
        shopEntity.setOperationStatus(0);
        shopEntity.setDelFlag(0);
        shopEntity.setCreateTime(new Date());
        BigDecimal bigDecimal = new BigDecimal(0);
        shopEntity.setAuditId(1);
        shopEntity.setMinPrice(bigDecimal);
        Integer integer = shopMapper.insert(shopEntity);
        if (integer > 0) {
            return CommonResult.ok(integer);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 根据用户id获取 名下所有店铺（门户商家版 展示）
    * @Author lzy
    * @Date 2022/10/18 9:04
    * @Return com.bgs.eat.config.CommonResult
    * @param userId
    */
    @Override
    public CommonResult getShopListByUserId(Integer userId) {
        ShopEntity shopEntity = new ShopEntity();
        shopEntity.setUserProtalId(userId);
        // 获取审核通过的
        shopEntity.setAuditId(2);
        List<ShopEntity> shopEntityList = shopMapper.select(shopEntity);
        return CommonResult.ok(shopEntityList);
    }

    /**
    * @Description 修改店铺营业状态（门户商家版 展示）
    * @Author lzy
    * @Date 2022/10/18 14:36
    * @Return com.bgs.eat.config.CommonResult
     * @param status
     * @param shopId
    */
    @Override
    public CommonResult editShopStatus(Integer status,Integer shopId) {
        ShopEntity shopEntity = new ShopEntity();
        shopEntity.setId(shopId);
        shopEntity.setOperationStatus(status);
        int i = shopMapper.updateByPrimaryKeySelective(shopEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 根据 店铺id获取店铺信息
    * @Author lzy
    * @Date 2022/10/19 9:04
    * @Return com.bgs.eat.config.CommonResult
    * @param shopId
    */
    @Override
    public CommonResult shopById(Integer shopId) {
        ShopEntity shopEntity = new ShopEntity();
        shopEntity.setId(shopId);
        ShopEntity shopEntity1 = shopMapper.selectOne(shopEntity);
        return CommonResult.ok(shopEntity1);
    }

    /**
    * @Author hbx
    * @Description 全查所有店铺信息
    * @Date 2022/10/20 9:43

    */
    @Override
    public List<ShopEntity> getAllShop() {
        Example example = new Example(ShopEntity.class);
        example.createCriteria().andEqualTo("delFlag",0).andEqualTo("auditId",2).andEqualTo("operationStatus",1);
        return shopMapper.selectByExample(example);
    }

    /**
    * @Author cjl
    * @Description 后台 店铺管理 全查（模糊查店铺名字
    * @Date  11:44
    * @Return java.util.List<com.bgs.eat.shop.entity.ShopEntity>
    * @param shopName
    */
    @Override
    public PageResult getAllShopEntityByShopName(String shopName,String roleId,String countyId,PageRequest pageRequest) {
        if (StringUtils.isEmpty(shopName)){
            shopName="";
        }
        if (Integer.parseInt(roleId) == 6){
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(ShopEntity.class);
            example.createCriteria().andEqualTo("delFlag",0)
                                    .andEqualTo("countyId",countyId)
                                    .andLike("shopName","%"+shopName+"%");
            List<ShopEntity> list = shopMapper.selectByExample(example);
            return MyPageHelper.pageInfo(list);
        } else {
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(ShopEntity.class);
            example.createCriteria().andEqualTo("delFlag",0)
                    .andLike("shopName","%"+shopName+"%");
            List<ShopEntity> list = shopMapper.selectByExample(example);
            return MyPageHelper.pageInfo(list);
        }
    }

    /**
    * @Author cjl
    * @Description 后台 下架店铺 删除标识变为 0  关闭店铺理由
    * @Date  8:27
    * @Return int
    * @param shopEntity
    */
    @Override
    public int updateShopEntityByDelFlag(ShopEntity shopEntity) {
        int i = 0;
        Example example = new Example(UserProtalEntity.class);
        example.createCriteria().andEqualTo("id",shopEntity.getUserProtalId());
        UserProtalEntity userProtalEntity = userProtalMapper.selectOneByExample(example);
        if (userProtalEntity.getMail()!= null && userProtalEntity.getMail()!= ""){
            String mail = userProtalEntity.getMail();
            String closeShopReason = shopEntity.getCloseShopReason();
            MailUtils.sendMail(mail,"吃了么","您的商铺下架"+"<br>"+"下架理由:"+closeShopReason);
        }
        shopEntity.setDelFlag(1);
        i = shopMapper.updateByPrimaryKeySelective(shopEntity);
        return i;
    }

    /**
    * @Author cjl
    * @Description 后台 回复店铺全查
    * @Date  8:28
    * @Return com.bgs.eat.page.PageResult
    * @param shopName
    * @param pageRequest
    */
    @Override
    public PageResult getAllShopEntityDelFlagZero(String shopName,String roleId,String countyId, PageRequest pageRequest) {
        if (StringUtils.isEmpty(shopName)){
            shopName="";
        }
        if (Integer.parseInt(roleId) == 6){
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(ShopEntity.class);
            example.createCriteria().andEqualTo("delFlag",1)
                    .andEqualTo("countyId",countyId)
                    .andLike("shopName","%"+shopName+"%");
            List<ShopEntity> list = shopMapper.selectByExample(example);
            return MyPageHelper.pageInfo(list);
        } else {
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(ShopEntity.class);
            example.createCriteria().andEqualTo("delFlag",1)
                    .andLike("shopName","%"+shopName+"%");
            List<ShopEntity> list = shopMapper.selectByExample(example);
            return MyPageHelper.pageInfo(list);
        }
    }
    /**
    * @Author cjl
    * @Description 后台 上架商铺
    * @Date  8:29
    * @Return int
    * @param shopEntity
    */
    @Override
    public int updateShopEntityByDelFlagZero(ShopEntity shopEntity) {
        int i = 0;
        Example example = new Example(UserProtalEntity.class);
        example.createCriteria().andEqualTo("id",shopEntity.getUserProtalId());
        UserProtalEntity userProtalEntity = userProtalMapper.selectOneByExample(example);
        if (userProtalEntity.getMail()!= null && userProtalEntity.getMail()!= ""){
            String mail = userProtalEntity.getMail();
            MailUtils.sendMail(mail,"吃了么","您的商铺已上架");
        }
        shopEntity.setDelFlag(0);
        shopEntity.setCloseShopReason("");
        i = shopMapper.updateByPrimaryKeySelective(shopEntity);
        return i;
    }

    /**
    * @Author cjl
    * @Description 审批店铺管理全查 根据地区展示店铺（后端）
    * @Date  9:55
    * @Return com.bgs.eat.page.PageResult
    * @param shopName
    * @param roleId
    * @param countyId
    * @param pageRequest
    */
    @Override
    public PageResult selectShopLeftJoinUserProtal(String shopName, Integer roleId, Integer countyId, PageRequest pageRequest) {
        if (StringUtils.isEmpty(shopName)){
            shopName="";
        }
        if (roleId == 6){
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(ShopEntity.class);
            example.createCriteria().andEqualTo("countyId",countyId)
                                    .andEqualTo("delFlag",0)
                                    .andEqualTo("auditId",1)
                                    .andLike("shopName","%"+shopName+"%");
            List<ShopEntity> list = shopMapper.selectByExample(example);
            return MyPageHelper.pageInfo(list);
        } else {
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(ShopEntity.class);
            example.createCriteria().andEqualTo("delFlag",0)
                                    .andEqualTo("auditId",1)
                                    .andLike("shopName","%"+shopName+"%");
            List<ShopEntity> list = shopMapper.selectByExample(example);
            return MyPageHelper.pageInfo(list);
        }
    }
    /**
    * @Author cjl
    * @Description 审批店铺通过
    * @Date  11:32
    * @Return int
    * @param shopEntity
    */
    @Override
    public int examineShopSucceed(ShopEntity shopEntity) {
        int i = 0;
        Example example = new Example(UserProtalEntity.class);
        example.createCriteria().andEqualTo("id",shopEntity.getUserProtalId());
        UserProtalEntity userProtalEntity = userProtalMapper.selectOneByExample(example);
        String mail = userProtalEntity.getMail();
        if (!mail.equals("") && !mail.equals("null") && mail != null){
            MailUtils.sendMail(mail,"吃了么","您的店铺申请已通过");
        }
        shopEntity.setAuditId(2);
        i = shopMapper.updateByPrimaryKeySelective(shopEntity);
        return i;
    }

    /**
     * @Author cjl
     * @Description 审批店铺失败（后端）
     * @Date  8:29
     * @Return com.bgs.eat.config.CommonResult
     * @param map
     */
    @Override
    public int examineShopFailed(Map<String, String> map) {
        String id = map.get("id");
        String userProtalId = map.get("userProtalId");
        String reason = map.get("reason");
        int i = 0;
        Example example = new Example(UserProtalEntity.class);
        example.createCriteria().andEqualTo("id",userProtalId);
        UserProtalEntity userProtalEntity = userProtalMapper.selectOneByExample(example);
        String mail = userProtalEntity.getMail();
        if (!mail.equals("") && !mail.equals("null") && mail != null){
            MailUtils.sendMail(mail,"吃了么","您的店铺申请已驳回"+"<br>"+"驳回理由:"+reason);
        }
        ShopEntity shopEntity = new ShopEntity();
        shopEntity.setId(Integer.parseInt(id));
        shopEntity.setAuditId(3);
        i = shopMapper.updateByPrimaryKeySelective(shopEntity);
        return i;
    }

    /**
     * @Author G胖
     * @Description 修改商户版店铺信息
     * @Date 2022-10-26 下午 01:50
     * @Return com.bgs.eat.config.CommonResult

     */
    @Override
    public Integer editShop(ShopEntity shopEntity) {
        return shopMapper.updateByPrimaryKeySelective(shopEntity);
    }

    /**
     * @Author G胖
     * @Description 修改门脸照id
     * @Date 2022-10-26 下午 03:23
     * @Return

     */
    @Override
    public Integer updateFacadeImageId(ShopEntity shopEntity) {
        return shopMapper.updateByPrimaryKeySelective(shopEntity);
    }

    /**
     * @Author G胖
     * @Description 修改店内照id
     * @Date 2022-10-26 下午 03:48
     * @Return

     */
    @Override
    public Integer updatelobbyImageId(ShopEntity shopEntity) {
        return  shopMapper.updateByPrimaryKeySelective(shopEntity);
    }

    @Override
    public Integer updateShopLogo(ShopEntity shopEntity) {
        return  shopMapper.updateByPrimaryKeySelective(shopEntity);
    }

    /**
    * @Description 根据店铺id获取店铺信息（门户用户端）
    * @Author lzy
    * @Date 2022/10/28 8:59
    * @Return com.bgs.eat.config.CommonResult
    * @param id
    */
    @Override
    public CommonResult getShopFormById(Integer id) {
        ShopEntity shopEntity = new ShopEntity();
        shopEntity.setId(id);
        ShopEntity shopEntity1 = shopMapper.selectOne(shopEntity);
        return CommonResult.ok(shopEntity1);
    }
}
